<?php

namespace Home\Controller;

use Think\Exception;

/*
create table bbs_content
(
    user_id     int           not null default 0 comment '作者ID',
    id          int auto_increment primary key,
    bbs_id      int           not null default 0 comment '所属圈子',
    head_img    varchar(255)  not null default '0' comment '封面图,多张用逗号隔开',
    title       char(30)  not null default '0' comment '帖子标题',
    content     varchar(9999) not null default '0' comment '帖子内容',
    img         varchar(999) not null default '0' comment '图片',
    video       varchar(255)  not null default '0' comment '视频',
    click       int           not null default 0 comment '点击量',
    like_nums   int           not null default 0 comment '赞量',
    hate_nums   int           not null default 0 comment '踩量',
    status      tinyint       not null default 0 comment '状态,0:审核中,1:正常,9:其他状态',
    create_time datetime comment '创建时间',
    update_time timestamp comment '修改时间'
) comment '论坛内容表';
 * */

class BbsContentController extends BaseController
{
    public function _initialize()
    {
        if (method_exists(get_parent_class(), '_initialize')) {
            parent::_initialize(); // TODO: Change the autogenerated stub
        }
        $this->model = 'bbs_content';
    }

    /*列表
     *
     * */
    public function index()
    {
        try {
            /*----------------------------------------凑查询条件-------------------------------------------------------*/
            $where = array();


            $where = array_filter($where);
            /*---------------------------------------开始查询----------------------------------------------------------*/
            $model = D($this->model);
            $data = $model
                ->where($where)
                ->select();
            if (!is_array($data)) {
                throw new Exception('查询失败!' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '查询成功';
            $this->arr['code'] = 1;
            $this->arr['data'] = $data;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*更新
     *
     * */
    public function update()
    {
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id!', 1);
            }
            $model = D($this->model);
            $data = $model->find($id);
            if (!is_array($data) || empty($data)) {
                throw new Exception('id有误,或数据为空', 2);
            }
            if (IS_GET) {
                $this->arr['data'] = $data;
            } elseif (IS_POST) {
                if (!$model->create() || !$model->where(array('id' => $id))->save()) {
                    throw new Exception('保存出错!', 3);
                }
                $this->arr['code'] = 1;
                $this->arr['msg'] = I('msg') ? I('msg') : '保存成功!' . $model->getLastInsID();
            } else {
                $this->arr['msg'] = '仅接受get和post传输, get查询,post提交! 其他方式一律不接受!';
            }
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }


    /*新增
     *
     * */
    public function add()
    {
        try {
            $model = D($this->model);
            if (!$model->create() || !$model->add()) {
                throw new Exception('新增失败,' . $model->getError());
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '新增成功,' . $model->getLastInsID();
            $this->arr['code'] = 1;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*列表
     *times
     * */
    public function del()
    {
        $a = new $this;
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id', 1);
            }
            $model = D($this->model);
            if ($nums = $model->delete($id) === false) {
                throw new Exception('操作失败,' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '删除' . $nums . '条数据';
            $this->arr['code'] = 1;

        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*审核帖子
     *
     * */
    public function audit()
    {
        try {
            $id = I('id');
            $status = I('status') ? I('status') : 1;
            if (!$id || $status) {
                throw new Exception('缺少id', 1);
            }
            $model = D($this->model);
            if ($model->where(array('id' => $id))->setField('status', $status)) {
                throw new Exception('操作失败,' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '操作成功';
            $this->arr['code'] = 1;

        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }
}